package com.guigu.www.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageInfo;
import com.guigu.www.pojo.Cell;
import com.guigu.www.pojo.File;
import com.guigu.www.pojo.PageVo;
import com.guigu.www.service.CellService;
import com.guigu.www.service.FileService;
import com.guigu.www.service.imp.IdUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

//动态库存
@RestController
public class CellController {
    @Autowired
    CellService cellService;
    @Autowired
    FileService fileService;
    //库存流水号
    @Autowired
    IdUtils idUtils;
    //获取当前时间给前端
    @GetMapping("queryCellByDate.action")
    public Map queryCellByDate(){
        Map map=new HashMap();
        //获取当前时间
        Date date=new Date();
        map.put("datetime",date.toLocaleString());
        return  map;
    }

    //添加安全库存配置单
    @PostMapping("/addCellStock.action")
    public Map addCellStock(Cell cell,String stackdz){
        //创建map
        Map map=new HashMap();

        System.out.println(stackdz);

        //创建对象
        String[] split = stackdz.split("-");
        cell.setFirstKindName(split[0]);
        cell.setSecondKindName(split[1]);
        cell.setThirdKindName(split[2]);
        cell.setFirstKindId("01");
        cell.setSecondKindId("01");
        cell.setThirdKindId("01");
        System.out.println(cell);
        //设置状态  //C001未审核  C002 审核通过  C003 审核不通过
        cell.setCheckTag("C001");
        ///=设置时间
        cell.setStoreId(idUtils.getid5(cell));
        cell.setAmount("0");
        //设置库存分配标志为K001-1
        fileService.updateDesign(cell.getProductId());

        //修改当前产品的设置状态
        fileService.updatestatus(cell.getProductId());
        //获取方法
        boolean save = cellService.save(cell);
        map.put("msg","安全库存配置单提交失败");
        //判断
        if(save){
            map.put("msg","安全库存配置单提交完成，等待审核");
        }
        return  map;
    }
    //查询审核的安全仓库配置单
    @RequestMapping("/queryByCellByCheckTag.action")
    public PageVo<Cell> queryByCellByCheckTag(@RequestParam(value = "pageno",defaultValue ="1")int pageno,
                                              @RequestParam(value = "pagesize",defaultValue = "5")int pagesize,
                                              Cell cell){
        //调用方法
        PageInfo<Cell> pageInfo = cellService.queryByCellByCheckTag(pageno, pagesize, cell);
        //创建pageVo
        PageVo<Cell> pageVo=new PageVo<Cell>();
        pageVo.setRows(pageInfo.getList());
        pageVo.setTotal(pageInfo.getTotal());
        System.out.println(pageInfo);
        System.out.println(pageInfo.getList());
        return pageVo;
    }

    //复核通过修改状态
    @RequestMapping("/queryByCekkUpdatestatus.action")
    public Map queryByCekkUpdatestatus(Cell cell){
        //获取map
        Map map=new HashMap();
        //获取修改状态方法
       boolean f= cellService.queryByCekkUpdatestatus(cell);
       //判断
        map.put("msg","复核失败");
        if(f){
            map.put("msg","复核成功");
        }
        return map;
    }


    //根据审核id查询
    @RequestMapping("/queryByCellIdcheck.action/{id}")
    public Cell queryByCellIdcheck(@PathVariable  int id){
        //获取方法
        Cell cell= cellService.getById(id);
        return cell;


    }

    //查询所以的制定安全库存的产品
    @GetMapping("/queryallcellshow.action")
    public PageVo<Cell> queryallcellshow(@RequestParam(value = "pageno",defaultValue = "1") int pageno,
                                         @RequestParam(value = "pagesize",defaultValue = "5")int pagesize,
                                         Cell cell){
        if(cell.getId()==null){
            cell.setId(-1);
        }
        //获取方法
        IPage<Cell> queryallcellshow = cellService.queryallcellshow(pageno, pagesize, cell);
        //获取pagevo
        PageVo<Cell> pageVo=new PageVo<Cell>();
        pageVo.setRows(queryallcellshow.getRecords());
        pageVo.setTotal(queryallcellshow.getTotal());
        return  pageVo;
    }

    //查询所有审核通过的产品
    @GetMapping("/queryCellByCellId.action")
    public PageVo<Cell> queryCellByCellId(@RequestParam(value = "pageno",defaultValue = "1") int pageno,
                                           @RequestParam(value = "pagesize",defaultValue = "5")int pagesize,
                                           Cell cell){
        if(cell.getProductName()==null){
            cell.setProductName("");
        }
        //创建pageVo
        PageVo<Cell> pageVo=new PageVo<Cell>();
        //查询所有审核通过的产品 变更
       PageInfo<Cell> page=cellService.queryCellByCellId(pageno,pagesize,cell);
       pageVo.setRows(page.getList());
       pageVo.setTotal(page.getTotal());
        return pageVo;
    }

    //变更
    @PutMapping("/updateCellByIdAll.action")
    public Map updateCellByIdAll(@RequestBody  Cell cell){

        System.out.println(cell);
        //设置状态
        cell.setCheckTag("C001");
      //创建map
       Map map=new HashMap();
       //获取方法
       boolean b = cellService.updateById(cell);
       //判断
       map.put("msg","变更失败");
       if(b){
           map.put("msg","变更成功");
        }
        return map;
    }

    //查询当前产品做了安全库存没
    @RequestMapping("/queryCellBycheckBytg.action")
    public Cell queryCellBycheckBytg(String productId){
        QueryWrapper<Cell> queryWrapper=new QueryWrapper<Cell>();
        queryWrapper.eq("CHECK_TAG","C002");
        queryWrapper.eq("PRODUCT_ID",productId);
        return cellService.getOne(queryWrapper);
    }

    //查询所以设置过动态库存的入库产品
    @RequestMapping("/dynamicinventory.action")
    public PageVo<Cell> dynamicinventory(@RequestParam(value = "pageno",defaultValue = "1") int pageno,
                                         @RequestParam(value = "pagesize",defaultValue = "5") int pagesize){
        //创建pagevo
        PageVo<Cell> pageVo=new PageVo<Cell>();
        //获取方法
        IPage<Cell> iPage=cellService.dynamicinventory(pageno,pagesize);

        //赋值
        pageVo.setRows(iPage.getRecords());
        pageVo.setTotal(iPage.getTotal());

        return  pageVo;
    }

    //查询所以审核通过的产品
    @RequestMapping("/queryByFileBychecktag.action")
    public PageVo<File> queryByFileBychecktag(@RequestParam(value = "pageno",defaultValue ="1") int pageno,
                                              @RequestParam(value = "pagesize",defaultValue = "5") int pagesize){
      //创建pagevo
      PageVo<File> pageVo=new PageVo<File>();
      //获取方法
      IPage<File> iPages=fileService.queryByFileBychecktag(pageno,pagesize);
      //赋值
        pageVo.setTotal(iPages.getTotal());
        pageVo.setRows(iPages.getRecords());
        return pageVo;
    }

    //根据产品编号查询查询产品
    @RequestMapping("/queryByProdutctid.action")
    public File queryByProdutctid(String productid){
        QueryWrapper<File> queryWrapper=new QueryWrapper<File>();
        queryWrapper.eq("PRODUCT_ID",productid);
        //获取方法
        return  fileService.getOne(queryWrapper);
    }

    //首页echarts报表查询仓库所有数据
    @RequestMapping("/queryCellByDatels.action")
    public List<Cell> queryCellByDateL() {
        return cellService.queryCellByDate();
    }


}
